package com.magold.background;

import java.util.LinkedList;
import java.util.List;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.IEntity;
import org.andengine.entity.scene.background.EntityBackground;
import org.andengine.entity.shape.IAreaShape;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.color.Color;

import com.magold.texture.TextureFactory;

/**
 * 
 * @author Eternity
 *
 */
public class MeshBackground extends EntityBackground {
	protected List<CellGroup> mCellGroups;
	protected int mCurrentVisibleCount;
	protected IAreaShape mAtomSprite;

	private MeshBackground(IEntity pEntity) {
		super(pEntity);
		setColor(new Color(0, 0, 0, 0));
		mCellGroups = new LinkedList<CellGroup>();
	}

	public static MeshBackground create(String pImagePath, VertexBufferObjectManager vbom) {
		ITextureRegion pRegion = TextureFactory.createRepeatingImage(pImagePath);
		Sprite backgroundSprite = new Sprite(0, 0, pRegion, vbom);
		
		MeshBackground b = new MeshBackground(backgroundSprite);
		ITextureRegion pAtomRegion = TextureFactory.createImage(pImagePath);
		b.mAtomSprite = new Sprite(0, 0, pAtomRegion, vbom);
		
		return b;
	}

	public void addCellGroup(CellGroup pGroup) {
		mCellGroups.add(pGroup);
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		super.onUpdate(pSecondsElapsed);

		for (CellGroup group : mCellGroups) {
			group.onUpdate(pSecondsElapsed);
		}
	}

	@Override
	public void onDraw(GLState pGLState, Camera pCamera) {
		pGLState.pushModelViewGLMatrix();

		super.onDraw(pGLState, pCamera);

		drawCellGroups(pGLState, pCamera);

		pGLState.popModelViewGLMatrix();
	}

	private void drawCellGroups(GLState pGLState, Camera pCamera) {
		for (CellGroup group : mCellGroups) {
			group.onDraw(pGLState, pCamera);
		}
	}


}
